<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>我的草稿</title>
    <#include "/pubHeadStyle.html">
</head>
<body>

<div style="height: 100%;">
    <div class="tablesize roles" style="padding: 10px">
        <table id="myDraft" lay-filter="myDraft"></table>
    </div>
</div>

<!--表格序列号-->
<script type="text/html" id="serialNum">
    {{d.LAY_TABLE_INDEX + 1}}
</script>

<script type="text/html" id="headerToolsBar">
    <div class="ccpui-enterSel">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm" lay-event="query">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            <span>删除</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="reset">
            <i class="layui-icon layui-icon-refresh"></i>
            <span>重置</span>
        </a>
    </div>
    <form class="layui-form" action="" lay-filter="myDraftQueryForm" style="margin-top:10px">
        <div class="table-top-filter">
            <div class="table-filter-alone">
                <label class="layui-form-label">流程定义名称</label>
                <input type="text" name="Q_processName_SL" id="Q_processName_SL" class="layui-input filter-value"
                       placeholder="流程定义名称"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">流程实例标题</label>
                <input type="text" name="Q_subject_SL" id="Q_subject_SL" class="layui-input filter-value"
                       placeholder="流程实例标题"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">创建时间</label>
                <input type="text" name="Q_createTime_DL" id="createTime" class="layui-input filter-value"
                       placeholder="开始时间"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label" style="text-align: center;">至</label>
                <input type="text" name="Q_endCreateTime_DG" id="endCreateTime" class="layui-input filter-value"
                       placeholder="结束时间"/>
            </div>
        </div>
    </form>
    </div>
</script>

<script type="text/html" id="myDraftBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs " lay-event="start">启动流程</a>
    {{#  if(d.tableName !=="" && d.dsAlias!==""){ }}
        <a class="layui-btn layui-btn-xs" lay-event="copy">复制</a>
    {{#  } }}
</script>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'laydate', 'util', 'commons', 'setter', 'totalFilter', 'common'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var $ = layui.$;
        var setter = layui.setter;
        var laydate = layui.laydate;
        var commons = layui.commons;
        var common = layui.common;
        var totalFilter = layui.totalFilter;

        var tableH = commons.tabHeight();

        loadTable(serverPath.workflowPath + setter.workflowLinkPath + 'processRun/list/myDraft');

        //第一个实例
        function loadTable(url) {
            table.render({
                elem: '#myDraft'
                , cellMinWidth: 80
                , title: '我的草稿'
                , height: tableH
                , url: url
                , method: 'post'
                ,defaultToolbar: ['filter', 'print']
                , toolbar: '#headerToolsBar'
                ,autoSort: false
                , page: true
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'subject', title: '流程实例标题', minWidth: 330,sort: true, align: 'center'}
                    , {field: 'processName', title: '流程定义名称', sort: true, align: 'center'}
                    , {field: 'creator', title: '创建人', sort: true, align: 'center'}
                    , {
                        field: 'createTime', title: '创建时间', minWidth: 200,sort: true, align: 'center', templet: function (d) {
                            return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
                        }
                    }, { title: '管理', toolbar: '#myDraftBar', width: 150, align: 'center'}
                ]]
                ,done:function () {
                    commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
                    common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel')
                }
            });
            totalFilter.init('myDraft');
        }
        // 监听行工具事件
        table.on('toolbar(myDraft)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("myDraft", "myDraftQueryForm", {where: {}}, false);
                    break;
                case 'reset':
                    common.tableConditionRender("myDraft", "myDraftQueryForm", {where: {}}, true);
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请先选择一行数据！');
                    } else {
                        var selectedIdArray = [];
                        for (var i = 0; i < data.length; i++) {
                            selectedIdArray.push(data[i].runId);
                        }
                        var selectedIds = selectedIdArray.join(",");
                        layer.confirm('确定删除当前草稿吗？', function () {
                            $.ajax({
                                url: serverPath.workflowPath + setter.workflowLinkPath + "processRun/delDraft?runId=" + selectedIds,
                                type: "DELETE",
                                dataType: 'json',
                                success: function (data) {
                                    if(data.code == 0){
                                        //成功后的提示
                                        layer.alert(data.msg);
                                        //执行重载
                                        table.reload('myDraft', {
                                            page: {curr: 1, limit: 10}
                                        });
                                        totalFilter.init('myDraft');
                                    }else {
                                        layer.alert(data.msg);
                                    }

                                },
                                error: function (XMLHttpRequest) {
                                    layer.alert("请求失败！" + XMLHttpRequest.status);
                                }
                            })
                        })
                    }
                    break;
            }
        });

        table.on('tool(myDraft)', function (obj) {
                var data = obj.data;
                var runId = data.runId;
                var isNeedReload=true;
                switch (obj.event) {
                    case 'del':
                        layer.confirm("确认要删除该草稿吗？", {title: '删除确认'}, function (index) {
                            $.ajax({
                                url: serverPath.workflowPath + setter.workflowLinkPath + "processRun/delDraft?runId=" + runId,
                                type: 'DELETE',
                                dataType: 'json',
                                success: function (data) {
                                    if(data.code == 0){
                                        //成功后的提示
                                        layer.alert(data.msg);
                                        //执行重载
                                        table.reload('myDraft', {
                                            page: {curr: 1, limit: 10}
                                        });
                                        totalFilter.init('myDraft');
                                    }else {
                                        layer.alert(data.msg);
                                    }

                                },
                                error: function (XMLHttpRequest) {
                                    layer.alert("请求失败！" + XMLHttpRequest.status);
                                }
                            });
                        });
                        break;
                    case 'start':
                        var url= serverPath.workflowPath  + setter.workflowLinkPath +'task/view/startFlowForm?runId='+runId;
                        layer.open({
                            type: 2,
                            title: "流程启动",
                            area: ['100%', '100%'],
                            content: url,
                            cancel: function () {
                                isNeedReload=false;
                            },
                            end:function () {
                                if (isNeedReload) {
                                    table.reload('myDraft', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                    totalFilter.init('myDraft');
                                }
                            }
                        });
                        break;
                    case 'copy':
                        layer.confirm("确认要复制该草稿吗？", {title: '复制'}, function () {
                            $.ajax({
                                dataType: 'json',
                                url: serverPath.workflowPath + setter.workflowLinkPath + "processRun/copyDraft?runId=" + runId,
                                success: function (data) {
                                    if(data.code == 0){
                                        //成功后的提示
                                        layer.alert(data.msg);
                                        //执行重载
                                        table.reload('myDraft', {
                                            page: {curr: 1, limit: 10}
                                        });
                                        totalFilter.init('myDraft');
                                    }else {
                                        layer.alert(data.msg);
                                    }

                                },
                                error: function (XMLHttpRequest) {
                                    layer.alert("请求失败！" + XMLHttpRequest.status);
                                }
                            });
                        });
                        break;
                }
            }
        );

        common.tableSort("myDraft",{show:window.Initialization});
        commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        window.Initialization = function () {
            commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        };
    });
</script>
<style>
    .layui-table-tool-temp {
    padding-right: 0px;
}
</style>
</body>
</html>
<link rel="stylesheet" href="${ctx}/css/font/iconfontie.css" media="all"/>
<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>